package cl.ucinf.progii.solemne1.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Conexion {

	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	
	public Conexion(){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost/progii?" + 
				"user=root&password=123456");
		} catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
	}
	
	public PreparedStatement getPreparedStatement(String sql){
		try {
			return conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ResultSet executeQuery(String sql){
		try {
			stmt = conn.createStatement();
			return  stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public void cerrar(){
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException sqlEx) { } // ignore
				rs = null;
			}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException sqlEx) { } // ignore
				stmt = null;
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException sqlEx) { } // ignore
				conn = null;
		}
	}
	
}
